//
//  EnterAccount.m
//  MUBATE
//
//  Created by sunbin on 2017/2/15.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "EnterAccount.h"
#import "EnterAccountView.h"
#import "ResetModel.h"
#import "Verification.h"
#import "Login.h"
@interface EnterAccount ()

@end

@implementation EnterAccount
{
    EnterAccountView *container;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    

}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view hideHUD];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    container = (EnterAccountView *)[NHUtils loadViewFormCtl:self view:[EnterAccountView LoadEnterAccountView]];

    // block 传值
    WEAKSELF;
    container.nextStepHandler = ^(NSString *account ,NSInteger index){
        [weakSelf.view endEditing:YES];
        switch (index) {
            case 0:{
                // 返回d登录页面
                [NHUtils presentViewController:@"Login" ctl:weakSelf animationType:WXSTransitionAnimationTypeSysPushFromLeft isSysBackAnimation:NO];
            }
                break;
            case 1:{
                // 找回密码
                [weakSelf sendCodeToAccount:account];}
                break;
            default:
                break;
        }        NSLog(@"account = %@" ,account);
    };
    

    
}

// 发送验证码 成功（跳转验证）与失败（提示）
- (void)sendCodeToAccount:(NSString *)account{

    WEAKSELF;
   accountType ty = [weakSelf getAccountType:account];
    if (ty == NoAccount) {
        [weakSelf notRegistered];
        return;
    }
    
    [self.view showLoading:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.view hideHUD];
        [ResetModel resetHandle:SenderCode handleValue:nil handleAccount:account accountTy:ty success:^(NSDictionary *loginresults, NSString *loginRequestmsg) {
            // 核对验证码
            Verification *verification = [[Verification alloc] init];
            verification.account = account;
            verification.ty = ty;
            [self presentViewController:verification animated:YES completion:^{
                [verification.view showLoadFinish:loginRequestmsg showLoc:bottom];
            }];
        } failure:^(NSDictionary *loginresults, responseState resMake, NSString *loginRequestError) {
            [weakSelf.view showError:loginRequestError];
            [weakSelf errorToClear];

        }];

    });
}

// 没有注册账户 （不是邮箱 手机号）
- (void)notRegistered{
    WEAKSELF;
    [self.view showLoading:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.view hideHUD];
        
        [weakSelf.view showError:NSLocalizedString(@"This user has not been registered", @"This user has not been registered")];
        [weakSelf errorToClear];

    });
    
}

- (void)errorToClear{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        container.account.text = nil;
        [container.account becomeFirstResponder];
        container.confirmBtn.enabled = NO;
    });

}


- (accountType)getAccountType:(NSString *)account{

    BOOL  isValidateEmail = [CommonUtils isValidateEmail:account];
    BOOL isValidatePhoneNum = [CommonUtils isValidatePhoneNum:account];
    accountType ty;
    if (isValidateEmail) {
        ty = EmailTYpe;
    }else if (isValidatePhoneNum){
        ty = MobilephoneType;
    }else{
        ty = NoAccount;
    }
    return ty;

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
